import store from '@/store/index'
import { apiInit } from '@/services'
import { LocaleEnum } from '@/locales'
import pcRouter from '@/entry_desktop/router'

export default {
	async checkPhone() {
		const api = apiInit()

		const user: any = {
			data: {
				// 默认先取cookies中的语言，cookies中取不到再取中文
				cookie_language_enum:
					(localStorage.getItem('locale') as any) ?? LocaleEnum.zh,
			},
		}

		// 判断是否登录
		const isLoginHref = window.location.href.indexOf('login') > -1

		const tenantDetail: any =
			JSON.parse(localStorage.getItem('tenantDetail') || '') ||
			(await api.baseAuto.GetTenantDetailTenantPublicApiPOST({}))

		if (
			!isLoginHref &&
			tenantDetail.data.tenant_auth.baiteda_security_account &&
			!(user.data.org_type === 'EXTERNAL')
		) {
			// const userCenter = await api.default.GetCenterUserUserCenterPrivateV1GET(
			// 	{}
			// )
			// store.commit('setCenterUser', userCenter.data ?? {})
			// localStorage.userCenter = JSON.stringify(userCenter.data ?? {})

			if (
				user.data?.employee_id !== 'sysadmin' &&
				tenantDetail.data.login_types === '0' &&
				!tenantDetail.data.is_private_environment &&
				tenantDetail.data.env_type === 'prd' &&
				tenantDetail.data.tenant_auth.baiteda_security_account &&
				!store.state.centerUser?.user_base_info &&
				!(user.data.org_type === 'EXTERNAL')
			) {
				pcRouter.push('/welcome')
			}
		}
	},
}
